package com.ssy.lingxi.marketing.serviceimpl.feign;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.json.JSONUtil;
import com.ssy.lingxi.common.exception.BusinessException;
import com.ssy.lingxi.common.response.PageData;
import com.ssy.lingxi.common.response.ResponseCode;
import com.ssy.lingxi.common.response.Wrapper;
import com.ssy.lingxi.marketing.service.feign.IProductFeignService;
import com.ssy.lingxi.marketing.utils.FeignLogUtils;
import com.ssy.lingxi.product.api.feign.ICommodityFeign;
import com.ssy.lingxi.product.api.model.request.feign.CommodityPriceRequest;
import com.ssy.lingxi.product.api.model.response.BrandResponse;
import com.ssy.lingxi.product.api.model.response.commodity.CommodityGoodsResponse;
import com.ssy.lingxi.product.api.model.response.commodity.CommodityOrderResponse;
import com.ssy.lingxi.product.api.model.response.commodity.CustomerCategoryResponse;
import com.ssy.lingxi.product.api.model.response.feign.CommodityPriceResponse;
import com.ssy.lingxi.search.api.feign.ITemplateFeign;
import com.ssy.lingxi.search.api.model.request.TemplateCommoditySearchRequest;
import com.ssy.lingxi.search.api.model.response.CommoditySearchResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


/**
 * 商品内部接口
 * @author yzc
 * @version 2.0.0
 * @date 2021/7/19
 */
@Slf4j
@Service
public class ProductFeignServiceImpl implements IProductFeignService {

    @Resource
    private ICommodityFeign commodityFeign;
    @Resource
    private ITemplateFeign templateFeign;

    @Override
    public Wrapper<List<CustomerCategoryResponse>> getCustomerCategoryById(List<Long> idList) {
        return commodityFeign.getCustomerCategoryById(idList);
    }

    @Override
    public Wrapper<List<BrandResponse>> getBrandById(List<Long> idList) {
        return commodityFeign.getBrandById(idList);
    }

    @Override
    public List<CommodityOrderResponse> getCommodityListByOrder(CommodityPriceRequest commodityPriceRequest){
        List<CommodityOrderResponse> resultList = new ArrayList();
        try {
            resultList = commodityFeign.getCommodityListByOrder(commodityPriceRequest);
        } catch (Exception e){
            log.error("调用商品服务失败, method: getCommodityListByOrder", e);
        }
        return resultList;
    }

    @Override
    public Wrapper<PageData<CommoditySearchResponse>> searchCommodityList(TemplateCommoditySearchRequest templateCommoditySearchRequest){
        return templateFeign.searchCommodityList(templateCommoditySearchRequest);
    }

    private CommodityPriceRequest buildCommodityPriceRequest(Long shopId, Integer commodityType, Long memberId, Long roleId, List<Long> skuIds){
        CommodityPriceRequest request=new CommodityPriceRequest();
        request.setShopId(shopId);
        request.setCommodityType(commodityType);
        request.setMemberId(memberId);
        request.setMemberRoleId(roleId);
        request.setCommoditySkuIdList(skuIds);
        return request;
    }

    @Override
    public Map<Long, Map<String, Double>> getCommodityPriceBatch(Long shopId,Integer commodityType,Long memberId,Long roleId,List<Long> skuIds) {
        CommodityPriceRequest request=buildCommodityPriceRequest(shopId,commodityType,memberId,roleId,skuIds);
        Map<Long, Map<String, Double>> commodityPriceBatch = commodityFeign.getCommodityPriceBatch(request);
        if(commodityPriceBatch==null||commodityPriceBatch.size()==0){
            log.info("计算到手价：查询商品价格为空，request{}", JSONUtil.toJsonStr(request));
        }
        return commodityPriceBatch;
    }

    @Override
    public Map<Long, CommodityPriceResponse> getCommodityPriceAndMemberPriceBatch(Long shopId,Integer commodityType,Long memberId,Long roleId,List<Long> skuIds) {
        CommodityPriceRequest request=buildCommodityPriceRequest(shopId,commodityType,memberId,roleId,skuIds);
        Map<Long, CommodityPriceResponse> commodityPriceBatch = commodityFeign.getCommodityPriceAndMemberPriceBatch(request);
        if(commodityPriceBatch==null||commodityPriceBatch.size()==0){
            log.info("计算到手价：查询商品价格与是否允许会员折扣为空，request{}", JSONUtil.toJsonStr(request));
        }
        return commodityPriceBatch;
    }

    @Override
    public Map<Long, CommodityGoodsResponse> getCommoditySkuMap(List<Long> skuIds, Long memberId, Long roleId, Integer status) {
        if(CollectionUtil.isEmpty(skuIds)){
            return null;
        }
        Wrapper<List<CommodityGoodsResponse>> commodityPriceBatch = commodityFeign.getCommodityByCommoditySkuIdList(skuIds.stream().distinct().collect(Collectors.toList()));
        FeignLogUtils.printLog("通过skuId批量查询商品明细",commodityPriceBatch,skuIds);
        if(commodityPriceBatch.getCode()==Wrapper.success().getCode()){
            if(CollectionUtil.isNotEmpty(commodityPriceBatch.getData())) {
                Map<Long, CommodityGoodsResponse> skuDataMap = new HashMap<>();
                boolean filterMember = (memberId != null && roleId != null);
                for (CommodityGoodsResponse c : commodityPriceBatch.getData()) {
                    //过滤状态
                    if (status != null && !c.getStatus().equals(status)) {
                        continue;
                    }
                    //过滤会员
                    if (filterMember) {
                        if (memberId.equals(c.getMemberId()) && roleId.equals(c.getMemberRoleId())) {
                            skuDataMap.put(c.getId(), c);
                        }
                    } else {
                        skuDataMap.put(c.getId(), c);
                    }
                }
                return skuDataMap;
            }else {
                return null;
            }
        }else{
            throw new BusinessException(ResponseCode.FEIGN_SERVICE_ERROR);
        }
    }


}
